package com.zwt.util;

import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * 正则表达式校验类
 * @author Autorun
 */
public class TRegular {

    private static final String PHONE_REG = "^((13[0-9])|(15[^4])|(18[0-9])|(19[0-9])|(17[0-8])|(147))\\d{8}$";
    private static final String MAIL_REG = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    private static final String NUNBER_REG = "^[0-9]*[1-9][0-9]*$";

    /**
     * 手机号正则校验
     * @author Autorun
     * @param phone
     * @return 是否校验通过
     */
    public static Boolean validatePhone(String phone){
        if (StringUtils.isEmpty(phone)) {
            return false;
        }
        if (phone.trim().length() != 11) {
            return false;
        }
        return phone.matches(PHONE_REG);
    }

    /**
     * 邮箱正则校验
     * @param mail
     * @return
     */
    public static Boolean validateEmail(String mail){
        if (StringUtils.isEmpty(mail)) {
            return false;
        }
        if (mail.trim().length() <= 3) {
            return false;
        }
        return mail.matches(MAIL_REG);
    }

    /**
     * 数字正则校验
     * @param number
     * @return
     */
    public static Boolean validateNumber(String number){
        if (StringUtils.isEmpty(number)) {
            return false;
        }
        if (number.trim().length() <= 3) {
            return false;
        }
        return number.matches(NUNBER_REG);
    }

    /**
     * 根据正则表达式校验
     * @param validateStr
     * @param regx
     * @return
     * @throws PatternSyntaxException
     */
    public static Boolean validateForRegular(String validateStr, final String regx) throws PatternSyntaxException {
        if (validateStr == null) {
            return false;
        }
        if (StringUtils.isEmpty(regx)) {
            return false;
        }
        try {
            return validateStr.matches(regx);
        } catch (PatternSyntaxException e) {
            throw e;
        }
    }
    /**
	 * 判断字符串是否含有中文
	 * @param str
	 * @return
	 */
	public static boolean isContainChinese(String str){
		Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
	}

    public static String replacePhone(String phone){
        if (phone != null) {
            phone = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
        }
        return phone;
    }
	public static void main(String[] args) {
//		System.out.println(isContainChinese("ajdgsc中jahs"));
//		System.out.println(isContainChinese("中ajdgscjahs"));
//		System.out.println(isContainChinese("ajdgscjahs中"));
//		System.out.println(isContainChinese("ajdgscjahs"));
//		System.out.println(isContainChinese("数据库代表集合adcd"));
//		System.out.println(isContainChinese("数据库代表集合"));
//		System.out.println(isContainChinese(""));
//		System.out.println(isContainChinese("s af vs gb"));
		System.out.println(validatePhone("19933400303"));
		System.out.println(validatePhone("18839971593"));
		System.out.println(validatePhone("12161617140"));
//		System.out.println(isContainChinese("CaiChang Internatioaaaaaaaaaaaaaaaaaaaaaanal"));
	}
}
